package com.wb.common;

import com.wb.util.FileUtil;
import com.wb.util.JsonUtil;
import com.wb.util.StringUtil;
import java.io.File;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.json.JSONArray;
import org.json.JSONObject;

public class Var {
	public static boolean uncheckModified;
	public static boolean sendStreamGzip;
	public static int sendGzipMinSize;
	public static boolean printError;
	public static boolean showSocketError;
	public static boolean log;
	public static boolean taskLog;
	public static int limitRecords;
	public static int limitExportRecords;
	public static int gzipMinSize;
	public static boolean cacheEnabled;
	public static int cacheMaxAge;
	public static boolean debug;
	public static boolean homeConsolePrint;
	public static boolean homeShowApp;
	public static boolean consolePrint;
	public static boolean serverConsolePrint;
	public static boolean batchUpdate;
	public static int sessionTimeout;
	public static boolean uniqueLogin;
	public static int ajaxTimeout;
	public static int maskTimeout;
	public static boolean useLocalTime;
	public static boolean recordSession;
	public static int stringAsText;
	public static String syncPath;
	public static String jndi;
	public static String language;
	public static String defaultLanguage;
	public static String urlEncoding;
	public static String sessionVars;
	public static boolean forceUpperCase;
	public static boolean checkFieldReadOnly;
	public static boolean useServletPath;
	public static boolean autoLogout;
	public static String emptyString;
	public static int fetchSize;
	public static JSONArray cssLinks;
	public static JSONArray jsLinks;
	public static final File file;
	public static ConcurrentHashMap<String, Object> buffer;

	static {
		file = new File(Base.path, "wb/system/var.json");
	}

	public static Object get(String name) {
		if (StringUtil.isEmpty(name)) {
			throw new NullPointerException("Var name \"" + name + "\" can not be blank");
		} else {
			Object val = buffer.get(name);
			if (val == null) {
				throw new NullPointerException("Var \"" + name + "\" does not exist");
			} else {
				return val;
			}
		}
	}

	public static String getString(String name) {
		return get(name).toString();
	}

	public static int getInt(String name) {
		Object val = get(name);
		if (val instanceof Integer) {
			return (Integer) val;
		} else {
			throw new RuntimeException("Var \"" + name + "\" is not an integer value");
		}
	}

	public static double getDouble(String name) {
		Object val = get(name);
		if (val instanceof Double) {
			return (Double) val;
		} else {
			throw new RuntimeException("Var \"" + name + "\" is not a double value");
		}
	}

	public static boolean getBool(String name) {
		Object val = get(name);
		if (val instanceof Boolean) {
			return (Boolean) val;
		} else {
			throw new RuntimeException("Var \"" + name + "\" is not a boolean value");
		}
	}

	public static synchronized void set(String name, Object value) {
		if (name == null) {
			throw new NullPointerException("Null variable name");
		} else if (value == null) {
			throw new NullPointerException("Null variable value");
		} else {
			try {
				JSONObject object = JsonUtil.readObject(file);
				Object valObject = JsonUtil.getValue(object, name, '.');
				if (!(valObject instanceof JSONArray)) {
					throw new RuntimeException("\"" + name + "\" is not a variable.");
				} else {
					JSONArray valArray = (JSONArray) valObject;
					valArray.put(0, value);
					FileUtil.syncSave(file, object.toString(2));
					buffer.put(name, value);
					loadBasicVars();
				}
			} catch (Throwable var5) {
				throw new RuntimeException(var5);
			}
		}
	}

	public static synchronized void load() {
		try {
			buffer = new ConcurrentHashMap();
			JSONObject object = JsonUtil.readObject(file);
			getValues(object, "");
			loadBasicVars();
		} catch (Throwable var1) {
			throw new RuntimeException(var1);
		}
	}

	private static void getValues(JSONObject object, String parentName) {
		Set<Entry<String, Object>> items = object.entrySet();
		Iterator var7 = items.iterator();

		while (var7.hasNext()) {
			Entry<String, Object> item = (Entry) var7.next();
			Object value = item.getValue();
			String name = parentName + (String) item.getKey();
			if (value instanceof JSONObject) {
				getValues((JSONObject) value, name + '.');
			} else {
				JSONArray jsonArray = (JSONArray) value;
				if ("double".equals(jsonArray.getJSONObject(2).opt("type"))) {
					buffer.put(name, ((Number) jsonArray.opt(0)).doubleValue());
				} else {
					buffer.put(name, jsonArray.opt(0));
				}
			}
		}

	}

	public static void loadBasicVars() {
		uncheckModified = !getBool("sys.cache.checkModified");
		sendStreamGzip = getBool("sys.sendStreamGzip");
		sendGzipMinSize = getInt("sys.sendGzipMinSize");
		printError = getBool("sys.printError");
		showSocketError = getBool("sys.network.showSocketError");
		log = getBool("sys.log");
		taskLog = getBool("sys.task.log");
		limitRecords = getInt("sys.controls.limitRecords");
		limitExportRecords = getInt("sys.controls.limitExportRecords");
		gzipMinSize = getInt("sys.cache.gzipMinSize");
		cacheEnabled = getBool("sys.cache.enabled");
		cacheMaxAge = getInt("sys.cache.maxAge");
		debug = getBool("sys.debug");
		homeConsolePrint = getBool("sys.app.homeConsolePrint");
		homeShowApp = getBool("sys.app.homeShowApp");
		consolePrint = getBool("sys.ide.consolePrint");
		serverConsolePrint = getBool("sys.serverConsolePrint");
		batchUpdate = getBool("sys.db.batchUpdate");
		sessionTimeout = getInt("sys.session.sessionTimeout");
		uniqueLogin = getBool("sys.session.uniqueLogin");
		ajaxTimeout = getInt("sys.session.ajaxTimeout");
		maskTimeout = getInt("sys.session.maskTimeout");
		useLocalTime = getBool("sys.locale.useLocalTime");
		recordSession = getBool("sys.session.recordSession");
		stringAsText = getInt("sys.db.stringAsText");
		syncPath = getString("sys.ide.syncPath");
		jndi = getString("sys.jndi.default");
		language = getString("sys.locale.language");
		defaultLanguage = getString("sys.locale.defaultLanguage");
		urlEncoding = getString("sys.locale.urlEncoding");
		sessionVars = getString("sys.session.sessionVars");
		forceUpperCase = getBool("sys.db.forceUpperCase");
		checkFieldReadOnly = getBool("sys.db.checkFieldReadOnly");
		useServletPath = getBool("sys.useServletPath");
		autoLogout = getBool("sys.session.autoLogout");
		emptyString = getString("sys.db.emptyString");
		fetchSize = getInt("sys.db.fetchSize");
		String value = getString("sys.app.cssLinks");
		String[] items;
		String item;
		int var3;
		int var4;
		String[] var5;
		if (StringUtil.isEmpty(value)) {
			cssLinks = null;
		} else {
			cssLinks = new JSONArray();
			items = StringUtil.split(value, ';');
			var5 = items;
			var4 = items.length;

			for (var3 = 0; var3 < var4; ++var3) {
				item = var5[var3];
				cssLinks.put(item.trim());
			}
		}

		value = getString("sys.app.jsLinks");
		if (StringUtil.isEmpty(value)) {
			jsLinks = null;
		} else {
			jsLinks = new JSONArray();
			items = StringUtil.split(value, ';');
			var5 = items;
			var4 = items.length;

			for (var3 = 0; var3 < var4; ++var3) {
				item = var5[var3];
				jsLinks.put(item.trim());
			}
		}

		if (getBool("sys.optimize")) {
			debug = false;
			homeConsolePrint = false;
			printError = false;
			consolePrint = false;
			serverConsolePrint = false;
			uncheckModified = true;
		}

	}
}